package br.inf.ufrgs.usixml4desktop.rendering.javadesktop.components;

import java.awt.Dimension;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;

import org.jdom.Element;

import br.inf.ufrgs.usixml4desktop.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4desktop.exceptions.RenderingErrorException;
import br.inf.ufrgs.usixml4desktop.rendering.UsiXMLElementRenderer;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.RendererFactory;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;

public class UsiXMLJavaDesktopWindowRenderer extends UsiXMLElementRenderer {

	private List<String> layoutManagers = null;
		
	public UsiXMLJavaDesktopWindowRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
		
		this.layoutManagers = new ArrayList<String>();
		this.layoutManagers.add("flowBox");
		this.layoutManagers.add("borderBox");
		this.layoutManagers.add("gridBox");
		this.layoutManagers.add("gridBagBox");
	}
	
	@Override
	public UsiXMLElementRenderer getNextRenderer() {
		return new UsiXMLJavaDesktopButtonRenderer(
				(UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer);
	}

	@Override
	public String getValidElementName() {
		return "window";
	}

	@Override
	public Object processRenderization(Element element) throws 
		ParsingErrorException,RenderingErrorException {
		
		String attributeName = element.getAttributeValue("name");
		int attributeWidth = 0;
		int attributeHeight = 0;
		try {
			attributeWidth = NumberFormat.getIntegerInstance().parse(element.getAttributeValue("width")).intValue();
			attributeHeight = NumberFormat.getIntegerInstance().parse(element.getAttributeValue("height")).intValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		JFrame renderedFrame = new JFrame();
		renderedFrame.setName(attributeName);
				
		JDesktopPane desktop = new JDesktopPane();
	    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
	    renderedFrame.setContentPane(desktop);
	    renderedFrame.setPreferredSize(new Dimension(attributeWidth,attributeHeight));
	    
	    renderedFrame = this.recurseRenderization(element,renderedFrame);

	    try {
			this.insertContent(element, renderedFrame, JFrame.class.getMethod("setTitle", new Class[]{String.class}));
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
				
		return renderedFrame;
	}

	private JFrame recurseRenderization(Element element, JFrame renderedFrame) 
		throws RenderingErrorException, ParsingErrorException {
		
		for (Object object : element.getChildren()) {
			Element child = (Element) object;
			
			String name = child.getName();
			
			if (this.layoutManagers.contains(name)){
			//its a layout manager	
				renderedFrame.setContentPane(RendererFactory.getInstance().
					getFirstLayoutManagerRenderer(
							(UsiXMLJavaDesktopInterfaceRenderer) this.usiXMLInterfaceRenderer).
						renderElement(child,renderedFrame.getContentPane()));
				
				
			}else{
			//its a component
			}
			
		}
		
		return renderedFrame;
		
	}

	@Override
	public boolean validateElement(Element element) {
		return true;
	}

}
